/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladoresJpa;

import controladoresJpa.exceptions.NonexistentEntityException;
import controladoresJpa.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import model.DicionarioMR;
import model.Esquema;
import model.AtributoMR;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Persistence;
import model.Tabela;

/**
 * Módulo MR
 * Classe de Entidade do Banco de Dados, gerada automaticamente, porém com certas modificações
 * @author Eduardo, Maylon e Kamilla
 */
public class DicionarioMRJpaController implements Serializable {

    public DicionarioMRJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;
public DicionarioMRJpaController(){
    if(emf==null)
        emf = Persistence.createEntityManagerFactory("unb-gama-threedsPU");
            }
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(DicionarioMR dicionarioMR) throws PreexistingEntityException, Exception {
        if (dicionarioMR.getAtributoMRList() == null) {
            dicionarioMR.setAtributoMRList(new ArrayList<AtributoMR>());
        }
        if (dicionarioMR.getTabelaList() == null) {
            dicionarioMR.setTabelaList(new ArrayList<Tabela>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Esquema nomeProjeto = dicionarioMR.getNomeEsquema();
            if (nomeProjeto != null) {
                nomeProjeto = em.getReference(nomeProjeto.getClass(), nomeProjeto.getNome());
                dicionarioMR.setNomeEsquema(nomeProjeto);
            }
            List<AtributoMR> attachedAtributoMRList = new ArrayList<AtributoMR>();
            for (AtributoMR atributoMRListAtributoMRToAttach : dicionarioMR.getAtributoMRList()) {
                atributoMRListAtributoMRToAttach = em.getReference(atributoMRListAtributoMRToAttach.getClass(), atributoMRListAtributoMRToAttach.getNome());
                attachedAtributoMRList.add(atributoMRListAtributoMRToAttach);
            }
            dicionarioMR.setAtributoMRList(attachedAtributoMRList);
            List<Tabela> attachedTabelaList = new ArrayList<Tabela>();
            for (Tabela tabelaListTabelaToAttach : dicionarioMR.getTabelaList()) {
                tabelaListTabelaToAttach = em.getReference(tabelaListTabelaToAttach.getClass(), tabelaListTabelaToAttach.getNome());
                attachedTabelaList.add(tabelaListTabelaToAttach);
            }
            dicionarioMR.setTabelaList(attachedTabelaList);
            em.persist(dicionarioMR);
            if (nomeProjeto != null) {
                nomeProjeto.getDicionarioMRList().add(dicionarioMR);
                nomeProjeto = em.merge(nomeProjeto);
            }
            for (AtributoMR atributoMRListAtributoMR : dicionarioMR.getAtributoMRList()) {
                atributoMRListAtributoMR.getDicionarioMRList().add(dicionarioMR);
                atributoMRListAtributoMR = em.merge(atributoMRListAtributoMR);
            }
            for (Tabela tabelaListTabela : dicionarioMR.getTabelaList()) {
                tabelaListTabela.getDicionarioMRList().add(dicionarioMR);
                tabelaListTabela = em.merge(tabelaListTabela);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findDicionarioMR(dicionarioMR.getId()) != null) {
                throw new PreexistingEntityException("DicionarioMR " + dicionarioMR + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(DicionarioMR dicionarioMR) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            DicionarioMR persistentDicionarioMR = em.find(DicionarioMR.class, dicionarioMR.getId());
            Esquema nomeProjetoOld = persistentDicionarioMR.getNomeEsquema();
            Esquema nomeProjetoNew = dicionarioMR.getNomeEsquema();
            List<AtributoMR> atributoMRListOld = persistentDicionarioMR.getAtributoMRList();
            List<AtributoMR> atributoMRListNew = dicionarioMR.getAtributoMRList();
            List<Tabela> tabelaListOld = persistentDicionarioMR.getTabelaList();
            List<Tabela> tabelaListNew = dicionarioMR.getTabelaList();
            if (nomeProjetoNew != null) {
                nomeProjetoNew = em.getReference(nomeProjetoNew.getClass(), nomeProjetoNew.getNome());
                dicionarioMR.setNomeEsquema(nomeProjetoNew);
            }
            List<AtributoMR> attachedAtributoMRListNew = new ArrayList<AtributoMR>();
            
            if( atributoMRListNew !=null)
            for (AtributoMR atributoMRListNewAtributoMRToAttach : atributoMRListNew) {
                atributoMRListNewAtributoMRToAttach = em.getReference(atributoMRListNewAtributoMRToAttach.getClass(), atributoMRListNewAtributoMRToAttach.getNome());
                attachedAtributoMRListNew.add(atributoMRListNewAtributoMRToAttach);
            }
            atributoMRListNew = attachedAtributoMRListNew;
            dicionarioMR.setAtributoMRList(atributoMRListNew);
            List<Tabela> attachedTabelaListNew = new ArrayList<Tabela>();
            
            if( tabelaListNew !=null)
            for (Tabela tabelaListNewTabelaToAttach : tabelaListNew) {
                tabelaListNewTabelaToAttach = em.getReference(tabelaListNewTabelaToAttach.getClass(), tabelaListNewTabelaToAttach.getNome());
                attachedTabelaListNew.add(tabelaListNewTabelaToAttach);
            }
            tabelaListNew = attachedTabelaListNew;
            dicionarioMR.setTabelaList(tabelaListNew);
            dicionarioMR = em.merge(dicionarioMR);
            if (nomeProjetoOld != null && !nomeProjetoOld.equals(nomeProjetoNew)) {
                nomeProjetoOld.getDicionarioMRList().remove(dicionarioMR);
                nomeProjetoOld = em.merge(nomeProjetoOld);
            }
            if (nomeProjetoNew != null && !nomeProjetoNew.equals(nomeProjetoOld)) {
                nomeProjetoNew.getDicionarioMRList().add(dicionarioMR);
                nomeProjetoNew = em.merge(nomeProjetoNew);
            }
            
            if( atributoMRListOld !=null)
            for (AtributoMR atributoMRListOldAtributoMR : atributoMRListOld) {
                if (!atributoMRListNew.contains(atributoMRListOldAtributoMR)) {
                    atributoMRListOldAtributoMR.getDicionarioMRList().remove(dicionarioMR);
                    atributoMRListOldAtributoMR = em.merge(atributoMRListOldAtributoMR);
                }
            }
            
            if( atributoMRListNew !=null)
            for (AtributoMR atributoMRListNewAtributoMR : atributoMRListNew) {
                if (!atributoMRListOld.contains(atributoMRListNewAtributoMR)) {
                    atributoMRListNewAtributoMR.getDicionarioMRList().add(dicionarioMR);
                    atributoMRListNewAtributoMR = em.merge(atributoMRListNewAtributoMR);
                }
            }
            
            if( tabelaListOld !=null)
            for (Tabela tabelaListOldTabela : tabelaListOld) {
                if (!tabelaListNew.contains(tabelaListOldTabela)) {
                    tabelaListOldTabela.getDicionarioMRList().remove(dicionarioMR);
                    tabelaListOldTabela = em.merge(tabelaListOldTabela);
                }
            }
            
            if( tabelaListNew !=null)
            for (Tabela tabelaListNewTabela : tabelaListNew) {
                if (!tabelaListOld.contains(tabelaListNewTabela)) {
                    tabelaListNewTabela.getDicionarioMRList().add(dicionarioMR);
                    tabelaListNewTabela = em.merge(tabelaListNewTabela);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = dicionarioMR.getId();
                if (findDicionarioMR(id) == null) {
                    throw new NonexistentEntityException("The dicionarioMR with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            DicionarioMR dicionarioMR;
            try {
                dicionarioMR = em.getReference(DicionarioMR.class, id);
                dicionarioMR.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The dicionarioMR with id " + id + " no longer exists.", enfe);
            }
            Esquema nomeProjeto = dicionarioMR.getNomeEsquema();
            if (nomeProjeto != null) {
                nomeProjeto.getDicionarioMRList().remove(dicionarioMR);
                nomeProjeto = em.merge(nomeProjeto);
            }
            List<AtributoMR> atributoMRList = dicionarioMR.getAtributoMRList();
            for (AtributoMR atributoMRListAtributoMR : atributoMRList) {
                atributoMRListAtributoMR.getDicionarioMRList().remove(dicionarioMR);
                atributoMRListAtributoMR = em.merge(atributoMRListAtributoMR);
            }
            List<Tabela> tabelaList = dicionarioMR.getTabelaList();
            for (Tabela tabelaListTabela : tabelaList) {
                tabelaListTabela.getDicionarioMRList().remove(dicionarioMR);
                tabelaListTabela = em.merge(tabelaListTabela);
            }
            em.remove(dicionarioMR);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<DicionarioMR> findDicionarioMREntities() {
        return findDicionarioMREntities(true, -1, -1);
    }

    public List<DicionarioMR> findDicionarioMREntities(int maxResults, int firstResult) {
        return findDicionarioMREntities(false, maxResults, firstResult);
    }

    private List<DicionarioMR> findDicionarioMREntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(DicionarioMR.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public DicionarioMR findDicionarioMR(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(DicionarioMR.class, id);
        } finally {
            em.close();
        }
    }

    public int getDicionarioMRCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<DicionarioMR> rt = cq.from(DicionarioMR.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
